<?php
/**
 * @name IndexController
 * @author 6p2b7gaqkyoe4aw\administrator
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
error_reporting(E_ALL^E_NOTICE^E_WARNING);
class AddressController extends Yaf_Controller_Abstract {
    private $arrConfig='';
    private $salt='';
	
	
	public function init(){
		
		$arrConfig = Yaf_Application::app()->getConfig();
		$this->arrConfig=$arrConfig;
		$this->salt=$arrConfig->database->params->salt;
		//$salt=$arrConfig->database->params->salt;
	}
	
	
	public function listAction(){
		$info = json_decode(file_get_contents("php://input"),true);
		$salt=$info['salt'] ? $info['salt'] : $_REQUEST['salt'];
	   if(!$salt){
			//exit(json_encode(array('status'=>1,'info'=>'登陆状态正常','data'=>$user)));
		    exit(json_encode(array('status'=>0,'info'=>'缺少参数')));
		}
		$where['salt= ?']=$salt;
		$usermodel=new UserModel();
		$user=$usermodel->getuser($where);
		if(!$user){
			//exit(json_encode(array('status'=>1,'info'=>'登陆状态正常','data'=>$user)));
		    exit(json_encode(array('status'=>0,'info'=>'登陆状态失败')));
		}
		unset($where);
		$where['userid=?']=$user['id'];
		$addressmodel = new AddressModel();
		$list = $addressmodel->getList($where);
		echo json_encode($list);
	}
	
	public function getAction(){
	     
	}
	
	public function addAction(){
		
		$info = json_decode(file_get_contents("php://input"),true);
		
		
// 		$info=array(
// 			'salt'=>'9c46955967bdfff0b66e8d65cdece7b4',
// 			'realname'=>'中国人',
// 			'mobile'=>'13921912978',
// 			'province'=>'江苏省',
// 			'city'=>'扬州市',
// 			'area'=>'邗江区',
// 			'address'=>'皇宫花园1栋文昌律师所楼上',
// 			'isdefault'=>'0',
// 		);
		
		//echo 123;
		//echo json_encode($info);
	 
		//{"salt":"9c46955967bdfff0b66e8d65cdece7b4","realname":"\u4e2d\u56fd\u4eba","mobile":"13921912977","id":3,"province":"\u6c5f\u82cf\u7701","city":"\u626c\u5dde\u5e02","area":"\u9097\u6c5f\u533a","address":"\u7687\u5bab\u82b1\u56ed1\u680b\u6587\u660c\u5f8b\u5e08\u6240\u697c\u4e0a","isdefault":"0"}
		$salt=$info['salt'] ? $info['salt'] : $_REQUEST['salt'];
		
		if(!$salt){
			//exit(json_encode(array('status'=>1,'info'=>'登陆状态正常','data'=>$user)));
			exit(json_encode(array('status'=>0,'info'=>'缺少参数')));
		}
		
		$where['salt= ?']=$salt;
		$usermodel=new UserModel();
		$user=$usermodel->getuser($where);
		if(!$user){
			//exit(json_encode(array('status'=>1,'info'=>'登陆状态正常','data'=>$user)));
		
			exit(json_encode(array('status'=>0,'info'=>'登陆状态失败')));
		}
		unset($where);
		
		
		if(!$info['realname']){exit(json_encode(array('status'=>0,'info'=>'收货人不得为空')));}
		if(!$info['mobile']){exit(json_encode(array('status'=>0,'info'=>'电话不得为空')));}
		if(!$info['address']){exit(json_encode(array('status'=>0,'info'=>'地址不得为空')));}
		$data = [
				'userid'             => $user['id'],
				'realname' => $info['realname'],
				'mobile'            => $info['mobile'],
				'province'        => $info['province'],
				'city'     => $info['city'],
				'area'            => $info['area'],
				'address'            => $info['address'],
				'isdefault'             => $info['isdefault'],
		];
		
		$model = new AddressModel();
		if($info['id']){
			
			$where['id = ?'] = $info['id'];
			$where['userid = ?'] = $user['id'];
			$addreasdd_one=	$model->getOne($where);
			if(!$addreasdd_one){
				exit(json_encode(array('status'=>0,'info'=>'地址ID错误')));
			}
			$rs = $model->addadress($data, $where);
		}else{
			$rs = $model->addadress($data);
		}
		//echo json_encode($rs);
		if($rs){
			exit(json_encode(array('status'=>1,'info'=>'新增编辑地址成功')));
		}
	}
	
	public function editAction(){
	
	}
	
	public function delAction(){
		$info = json_decode(file_get_contents("php://input"),true);
		$salt=$info['salt'] ? $info['salt'] : $_REQUEST['salt'];
		$id=$info['id'] ? $info['id'] : $_REQUEST['id']; 
		if(!$salt || !$id){
			//exit(json_encode(array('status'=>1,'info'=>'登陆状态正常','data'=>$user)));
			exit(json_encode(array('status'=>0,'info'=>'缺少参数')));
		}
		$usermodel=new UserModel();
		$where['salt= ?']=$salt;
		$user=$usermodel->getuser($where);
		if(!$user){
			exit(json_encode(array('status'=>0,'info'=>'登陆状态失败')));
		}
		unset($where);
		$where['id = ?'] = $id;
		$where['userid = ?'] = $user['id'];
		$addressmodel = new AddressModel();
		
		$rs = $addressmodel->deladress($where);
		if($rs){
			exit(json_encode(array('status'=>1,'info'=>'删除成功')));
		}else{
			
		}exit(json_encode(array('status'=>1,'info'=>'删除失败')));
		
	}
	
	public function statusAction(){
	
	}
	
	
	
}
